/**
 * @api post control.
 *
 * User: yunshengzhu
 * Date: 2020/12/7
 * Time: 下午6:02
 */
package control

import (
	"card-platform/config"
	"card-platform/model"
	"encoding/json"
	"pkg/cfg"
)

type Logistics struct {
}

// swagger:operation POST /createLogistics 收货地址 createLogisticsReq
// ---
// summary: 添加收货地址 createLogistics.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/okResp"
func (a *Logistics) CreateLogistics(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := CreateLogisticsReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	logistics, err := model.GetLogisticsUserIdAndId(userId, req.LogisticsId)
	logistics.UserId = userId
	logistics.Address = req.Address
	logistics.Name = req.Name
	logistics.Mobile = req.Mobile
	logistics.ProvinceName = req.ProvinceName
	logistics.CityName = req.CityName
	logistics.CountyName = req.CountyName
	logistics.TownName = req.TownName
	logistics.AreaCode = req.AreaCode
	logistics.PostalCode = req.PostalCode
	logistics.Main = req.Main
	if logistics.LogisticsId > 0 {
		err = model.UpdateLogistics(logistics)
	} else {
		err = model.CreateLogistics(logistics)
	}

	return nil, config.OK, err
}

// swagger:operation POST /delLogistics 收货地址 delLogisticsReq
// ---
// summary: 删除收货地址 delLogistics.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/okResp"
func (a *Logistics) DelLogistics(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := DelLogisticsReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}
	err = model.DelLogistics(req.LogisticsId)
	return nil, config.OK, err
}

// swagger:operation POST /logisticsList 收货地址 logisticsListReq
// ---
// summary: 获取收货地址列表 logisticsList.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/logisticsListResp"
func (a *Logistics) LogisticsList(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	list, err := model.GetLogisticsList(userId)
	return list, config.OK, err
}

// swagger:operation POST /logisticsDetails 收货地址 logisticsDetailsReq
// ---
// summary: 获取地址详情 logisticsDetails.
// description: .
//// responses:
////   "200":
////     "$ref": "#/responses/logisticsDetailsResp"
func (a *Logistics) LogisticsDetails(userId int64, data []byte, param Param) (out interface{}, code int, err error) {
	req := LogisticsDetailsReq{}
	err = json.Unmarshal(data, &req)
	if err != nil {
		cfg.LogErr("err:", err)
		return nil, config.ERR_JSON, err
	}

	obj, err := model.GetLogisticsUserIdAndId(userId, req.LogisticsId)
	return obj, config.OK, err
}
